(function($){
	$.multicheck = function(el, options){
		// To avoid scope issues, use 'base' instead of 'this'
		// to reference this class from internal events and functions.
		var base = this;

		// Access to jQuery and DOM versions of element
		base.$el = $(el);
		base.el = el;

		// Add a reverse reference to the DOM object
		base.$el.data("multicheck", base);

		base.init = function(){	
			base.options = $.extend({},$.multicheck.defaultOptions, options);

			// Put your initialization code here
			var box = $('<a class="multicheck"></a>')
			.text(base.createText(base.$el.find('option[selected]')))
			.bind('click', function() {
				if ( $(this).next(".multicheck-options").size() > 0 ) {
					$(this).next(".multicheck-options").remove();
					return false;
				}

				var options = $('<div class="multicheck-options"></div>')
				.css('top', $(this).position().top + $(this).outerHeight())
				.css('left', $(this).position().left )
				.css('min-width', $(this).outerWidth() - 2)

				base.$el.find('option').each(function() {
					var opt = $('<a name="'+$(this).val()+'" href="#">'+$(this).text()+'</a>')
					.bind('click', function() {
						if ( $(this).hasClass("selected") ) {
							base.$el.find("option[value="+$(this).attr("name")+"]").attr("selected", false);
							$(this).removeClass("selected");
						}
						else {
							base.$el.find("option[value="+$(this).attr("name")+"]").attr("selected", true);
							$(this).addClass("selected");
						}
						box.text(base.createText(base.$el.find('option[selected]')));
						base.$el.trigger('change');
						return false;
					})
					.appendTo(options);

					if ( $(this).attr("selected") ) {
						opt.addClass("selected");
					}
				});

				$("body:not(multicheck-options)").bind("click", function() {
					if ( box.next(".multicheck-options").size() > 0 ) {
						box.next(".multicheck-options").remove();
					}
				});

				options
				.slideDown('fast')
				.insertAfter($(this))

				return false;
			});


			base.$el
			.after(box)
			.hide();
		};

		// Sample Function, Uncomment to use
		base.createText = function(options){
			if ( options.size() > 0 ) {
				var t = "";

				options.each(function() {
					t += $(this).text() + ",";
				});

				t = t.substring(0,t.length-1);
				if (t.length > 30) {
					t = t.substr(0,25) + "...";
				}

				return t;
			}
			else {
				return "Choix...";
			}
		};

		// Run initializer
		base.init();
	};

	$.multicheck.defaultOptions = {
	};

	$.fn.multicheck = function(options){
		return this.each(function(){
			(new $.multicheck(this, options));
		});
	};

})(jQuery);
